seajs.use(['zepto', 'calendar30', 'dialog', 'manager', 'cookie'], function ($, Calendar, dialog, Manager, CookieClass) {
    function setWebQrScanP(productId) {
        if (productId > 0) {
            var hash = decodeURIComponent(window.location.hash.replace(/^#/, ''));
            var result = /qrp=(\d+)/.exec(hash), p;
            if (result && result.length >= 2) {
                p = result[1];
                if (p > 0) {
                    var str = tuniu_common.base64.encode(productId + '_' + p);
                    document.cookie = 'qr_p=' + str + ';expires=' + new Date(new Date().getTime() + 60 * 20 * 1000).toGMTString() + ';domain=.tuniu.com;path=/';
                }
            }
        }
    };
    setWebQrScanP(routeInfo.routeId);
    dialog({type: 'info', align: 'center', message: "请选择您所要出游的团期"});
    localStorage.setItem("step", 1);
    var urlSplit = "/olb/order/gt/1/eyJyb3V0ZUlkIjo5NDE3ODJ9".split("/");
    var manager = new Manager();
    var Cookie = new CookieClass();
    var urlParam = JSON.parse(manager.decode(urlSplit[urlSplit.length - 1]));
    var adultNum = parseInt($(".tn-dr input:eq(0)").val());
    var childNum = parseInt($(".tn-dr input:eq(1)").val());
    setNumStr(adultNum, childNum);
    var device = parseInt(Cookie.getCookie("deviceType"), 10);
    $(".numInfo").css({"position": "fixed", "z-index": "40", "top": $('#topHeader').height() + "px"});
    $(".tn-dr").delegate("div", "tap", function (e) {
        var aStr = "{{aNum}}成人";
        var cStr = "";
        var ele = e.target;
        var parent = $(ele).parent();
        var index = parent.index();
        var input = parent.find("input");
        if (!$(ele).attr('class'))return;
        if ($(ele).hasClass("icon-question") || $(ele).hasClass("question")) {
            $(".msg").toggle();
            $(".triangle-content").html(routeInfo.childPriceIntro);
        }
        ;
        if ($(ele).parents('.tn-dr').hasClass('disabled')) {
            return;
        }
        if ($(ele).hasClass("minus")) {
            if (parseInt(input.val()) <= 0) {
                return;
            }
            ;
            if (index == 0 && parseInt(input.val()) == 1) {
                return;
            }
            input.val(parseInt(input.val()) - 1);
        }
        if ($(ele).hasClass("plus")) {
            input.val(parseInt(input.val()) + 1);
        }
        ;
        if (index == 0) {
            var aNum = input.val();
            aStr = aStr.replace("{{aNum}}", aNum);
            adultNum = parseInt(aNum);
            $(".select-info span:eq(0)").html(aStr);
        } else {
            if ($(ele).hasClass("icon-question")) {
                return;
            }
            ;
            var cNum = parseInt(input.val());
            if (cNum == 0) {
                cStr = "";
            } else {
                cStr = "，{{cNum}}儿童";
                cStr = cStr.replace("{{cNum}}", cNum);
            }
            $(".select-info span:eq(1)").html(cStr);
            childNum = parseInt(cNum);
        }
    });
    $("#submit").unbind("click").bind("click", submitFun);
    function setNumStr(aNum, cNum) {
        $(".select-info span:eq(0)").html(aNum + "成人");
        if (cNum != 0) {
            $(".select-info span:eq(1)").html("，" + cNum + "儿童");
        }
    }

    function getMonth(key) {
        return parseInt(key.split("-")[1]);
    }

    var departDatesInfo = [];
    var getDateInfo = {};
    for (var key in calendarInfo) {
        for (var date in calendarInfo[key]) {
            departDatesInfo.push({"date": date, "price": calendarInfo[key][date].startPrice});
            getDateInfo[date] = calendarInfo[key][date];
        }
    }
    var selectDate = "";
    var calendar = new Calendar().init();
    calendar.afterPickedDate = function (date) {
        var dateInfo = getDateInfo[date];
        selectDate = date;
        var showPriceStr = "";
        var endPriceStr = "最低优惠价<span>￥{{endPrice}}</span> = ";
        var tuniuPriceStr = "途牛价<span>￥" + dateInfo.adultPrice + "</span>";
        var maxPriceStr = "－最大优惠金额<span>￥{{maxPrice}}</span>";
        var vouchersPriceStr = "－可用抵用券<span>￥{{vouchersPrice}}</span>";
        $(".select-info div:eq(1)").html("已选团期：" + date).css({"color": "#ff7800"});
        $(".price-show").show();
        $("#adultPrice").html("￥" + dateInfo.adultPrice);
        $("#childPrice span:eq(0)").html("￥" + dateInfo.childPrice);
        $("#childPrice span:eq(1)").show();
        if (parseInt(dateInfo.childPrice) == 0 || dateInfo.childPrice.length == 0) {
            $("#childPrice span:eq(0)").html("不支持儿童");
            $("#childPrice span:eq(1)").hide();
            var cNum = parseInt($('.tn-dr:eq(1)').find("input").val());
            if (cNum != 0) {
                $(".select-info span:eq(1)").html("");
                $('.tn-dr:eq(1)').find("input").val(0)
                childNum = 0;
            }
            $('.tn-dr:eq(1)').addClass('disabled');
            dialog({type: 'info', message: '本线路团期不接受儿童出游'});
        } else {
            $('.tn-dr:eq(1)').removeClass('disabled');
        }
        $(".noty-show p:eq(1)").html('<section style="max-height:300px">' + dateInfo.promotionIntro + '</section>');
        $(".noty-show div").show();
        if (dateInfo.discountDesc && dateInfo.discountDesc.length > 0) {
            showPriceStr = showPriceStr + endPriceStr + tuniuPriceStr
            var endPrice = dateInfo.adultPrice;
            var maxPrice = 0;
            var vouchersPrice = 0;
            for (var i = 0; i < dateInfo.discountDesc.length; i++) {
                if (dateInfo.discountDesc[i].discountType == 0) {
                    showPriceStr = showPriceStr + maxPriceStr;
                    endPrice = endPrice - dateInfo.discountDesc[i].discountPrice;
                    maxPrice = dateInfo.discountDesc[i].discountPrice;
                    showPriceStr = showPriceStr.replace("{{maxPrice}}", maxPrice);
                }
                if (dateInfo.discountDesc[i].discountType == 1) {
                    showPriceStr = showPriceStr + vouchersPriceStr;
                    endPrice = endPrice - dateInfo.discountDesc[i].discountPrice;
                    vouchersPrice = dateInfo.discountDesc[i].discountPrice;
                    showPriceStr = showPriceStr.replace("{{vouchersPrice}}", vouchersPrice);
                }
            }
            showPriceStr = showPriceStr.replace("{{endPrice}}", endPrice);
            $(".lowest-price").html(showPriceStr);
        }
        $("#submit").removeClass("unactive");
        $("#submit").addClass("active");
        $(".numInfo").css({"padding": "19px 0 1px 0"});
        $(".noty-show p").show();
        $(".noty-show i").removeClass("icon-arrow-down");
        $(".noty-show i").addClass("icon-arrow-up");
        $(".msg").hide();
        if (showPriceStr.length == 0) {
            $(".noty-show").hide();
            $(".price-show").css({"border": "none"});
        } else {
            $(".noty-show").show();
            $(".price-show").css({"border": ""});
        }
        $("#submit").html("下一步").removeClass('unactive');
    }
    var count = 0;
    $(".noty-show").delegate("div", "click", function (e) {
        $(this).parent().find("p").toggle();
        var i = $(this).find("i");
        if (count++ % 2 == 0) {
            i.attr("class", "icon-arrow-down");
        } else {
            i.attr("class", "icon-arrow-up");
        }
    })
    function computeTotalPrice(date, adultNum, childNum) {
        var datas = getDateInfo[date], _adultNum = adultNum || 0, _childNum = childNum || 0, result;
        if (datas) {
            result = (datas.adultPrice || 0) * _adultNum + (datas.childPrice || 0) * _childNum
        }
        return result;
    }

    function submitFun(e) {
        if ($(this).hasClass('unactive') || selectDate.length == 0) {
            return;
        } else {
            var submitData = {routeId: routeInfo.routeId, beginDate: selectDate, adultNum: adultNum, childNum: childNum, bookId: routeInfo.bookId, productType: 1, totalPrice: computeTotalPrice(selectDate, adultNum, childNum)}
            submitData = manager.encode(JSON.stringify(submitData));
            $(this).html("处理中...").addClass("unactive");
            localStorage.setItem("step", 2);
            if (isAbroad) {
                location.href = $('#goto2GT').val() + "/params/" + submitData;
            } else {
                location.href = $('#goto2').val() + "/" + submitData;
            }
        }
    }

    $(document).bind("scroll", function (e) {
        if (selectDate.length == 0) {
            return;
        }
        $(".noty-show p").hide();
        $(".noty-show i").removeClass("icon-arrow-up");
        $(".noty-show i").addClass("icon-arrow-down");
        $(".msg").hide();
    });
    var timer = setInterval(function (argument) {
        var step = localStorage.getItem("step");
        if (parseInt(step) > 1) {
            $("#submit").html("下一步").removeClass('unactive');
        }
    }, 500)
});